package com.yl.immodule.fragment.model;

import android.util.Log;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.android.yl.ylicpsdk.ICPRtcEventHandler;
import com.android.yl.ylicpsdk.ICPRtcManager;
import com.camera.base.v.models.BaseViewModel;
import com.unionbroad.greendao.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ContactsViewModel extends BaseViewModel {
    private static final String TAG = "ContactsViewModel";
    private final MutableLiveData<List<User>> _userList = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<List<User>> _selectedUsers = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<Boolean> _isLoading = new MutableLiveData<>(false);
    public final LiveData<List<User>> userList = _userList;
    public final LiveData<List<User>> selectedUsers = _selectedUsers;
    public final LiveData<Boolean> isLoading = _isLoading;
    private final ICPRtcEventHandler eventHandler = new ICPRtcEventHandler() {
        @Override
        public void onUserStatusChanged(List<User> changedUsers) {
            loadUsers();
        }
    };
    
    public ContactsViewModel() {
        try {
            if (ICPRtcManager.getsInstance() != null) {
                ICPRtcManager.getsInstance().addEventHandler(eventHandler);
                loadUsers();
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void loadUsers() {
        _isLoading.setValue(true);
        
        try {
            List<User> users = ICPRtcManager.getsInstance().getContactsUserList();

            // 创建一个新的列表，并排序
            List<User> newList = new ArrayList<>(users);
            Collections.sort(newList, new UserStatusComparator());
            
            // 设置新列表
            _userList.setValue(newList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            _isLoading.setValue(false);
        }
    }
    
    public void toggleUserSelection(User user) {
        List<User> currentSelection = new ArrayList<>(_selectedUsers.getValue() != null ? 
                _selectedUsers.getValue() : new ArrayList<>());
        
        if (currentSelection.contains(user)) {
            currentSelection.remove(user);
        } else {
            currentSelection.add(user);
        }
        _selectedUsers.setValue(currentSelection);
    }
    
    public void clearSelection() {
        _selectedUsers.setValue(new ArrayList<>());
    }
    
    private static class UserStatusComparator implements Comparator<User> {
        @Override
        public int compare(User lhs, User rhs) {
            return (lhs.getStatus() == rhs.getStatus() ? 0 : (lhs.getStatus() > rhs.getStatus() ? -1 : 1));
        }
    }
    
    @Override
    protected void onCleared() {
        super.onCleared();
        try {
            if (ICPRtcManager.getsInstance() != null) {
                ICPRtcManager.getsInstance().removeEventHandler(eventHandler);
            }
        } catch (Exception e) {
        }
    }
} 